#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,ans;
pair<int,int> m[5];
struct node{
	int a,b,c;
}x[100010]; 
int read(){
	int s=0;
	char ch=getchar(),last=' ';
	while(ch<'0'||ch>'9')
		last=ch,ch=getchar();
	while(ch>='0'&&ch<='9')
		s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
	return last=='-'?-s:s;
}
int cmp(node x,node y){
	if(x.c==y.c)
		return x.b>y.b;
	return x.c<y.c;
}
int cmpp(node x,node y){
	return x.b<y.b;
}
signed main(){
	freopen("boast.in","r",stdin);
	freopen("boast.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++)
		x[i].a=read(),x[i].b=read(),x[i].c=x[i].a+x[i].b;
	if(n<=100){
		ans=1e18;
		sort(x+1,x+1+n,cmpp);
		for(int i=1;i+2<=n;i++)
			for(int j=i+1;j+1<=n;j++)
				for(int l=j+1;l<=n;l++)
					ans=min(ans,x[i].c+x[j].c+x[l].c-x[i].b);
	}
	else{
		sort(x+1,x+1+n,cmp);
		for(int i=1;i<=3;i++){
			ans+=x[i].c;
			m[i].first=x[i].b,m[i].second=i;
		}
		sort(m+1,m+4);
		ans-=m[1].first;
		for(int i=4;i<=n;i++){
			int k=ans-x[m[1].second].a;
			if(k+x[i].c-min(x[i].b,m[2].first)<ans)
				ans=k+x[i].c-min(x[i].b,m[2].first),m[1].first=x[i].b,m[1].second=i;
			sort(m+1,m+4);
		}
	}
	printf("%lld",ans);;
    return 0;
}


